package icraf.carbon.gui;

import icraf.carbon.core.AbacusContainer;
import icraf.carbon.core.CostBenefitUnit;
import icraf.carbon.core.Project;
import icraf.carbon.core.Zone.DataType;

import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class InputNPVPanel extends JPanel {
	private static final long serialVersionUID = 1122919881488352700L;
	private JTabbedPane tabPanel;
	private Project project;
	private JLabel label = new JLabel();

	public AbacusContainer getProject() {
		return project;
	}

	public void setProject(Project project, DataType t) {
		if (project == null)
			return;
		if (project.equals(this.project))
			return;
		this.project = project;
	
		ArrayList<CostBenefitUnit> cbu = project.getCostBenefitUnitList();
		for (int i = 0; i < cbu.size(); i++) {
			MatrixPanel lcPanel;
			if (i < tabPanel.getTabCount()) {
				lcPanel = (MatrixPanel) tabPanel.getComponentAt(i);
				tabPanel.setTitleAt(i, cbu.get(i).getLabel());
			} else {
				lcPanel = new MatrixPanel(new LCZTableModel());
				tabPanel.addTab(cbu.get(i).getLabel(), lcPanel);
			}
			LCZTableModel m = (LCZTableModel)lcPanel.getMatrixTableModel();
			m.setProject(project, t, cbu.get(i));
			m.setNegativeAllowed(true);
		}
		int n = tabPanel.getTabCount() - cbu.size();
		for (int i = 0; i < n; i++) {
			tabPanel.remove(cbu.size());
		}
	}

	public InputNPVPanel() {
		tabPanel = new JTabbedPane();
		this.setLayout(new BorderLayout());
		this.add(tabPanel);
		this.add(label, BorderLayout.NORTH);
	}

	public String getLabel() {
		return label.getText();
	}

	public void setLabel(String label) {
		this.label.setText(label);
	}



	public void setEditable(boolean editable) {
		MatrixPanel lcPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			lcPanel = (MatrixPanel) tabPanel.getComponentAt(i);
			((LCTableModel)lcPanel.getMatrixTableModel()).setEditable(editable);
		}
	}
	
	public void setNegativeAllowed(boolean isNegativeAllowed) {
		MatrixPanel lcPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			lcPanel = (MatrixPanel) tabPanel.getComponentAt(i);
			((LCTableModel)lcPanel.getMatrixTableModel()).setNegativeAllowed(isNegativeAllowed);
		}
	}
}
